//
//  NavigationController.swift
//  MQLWeibo
//
//  Created by MQL-IT on 2016/10/23.
//  Copyright © 2016年 MQL-IT. All rights reserved.
//

import UIKit

class NavigationController: UINavigationController {
    override func viewDidLoad() {
        //隐藏默认的NavigationBar
        navigationBar.isHidden = true
    }
    
    
    //重写push方法,所有的push都会调用此方法
    //viewController,是被push的控制器,设置它的左侧按钮作为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        print(viewController)
        //如果不是栈底控制器才需要隐藏,根控制器不需要处理
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            //判断控制器的类型
            if let vc = viewController as? BaseViewController {
                var title = "返回"
                //判断控制器的级数,只有一个控制器的时候,显示栈底控制器的标题
                if childViewControllers.count == 1 {
                    
                    //title显示首页的标题
                    title = childViewControllers.first?.title ?? "返回"
                }
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popParent), isBackButton: true)
            }
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    //POP返回到上级界面
    @objc private func popParent() {
        popViewController(animated: true)
    }

}
